# -*- coding:utf-8 -*-

# @Time     :2024/01/18/15/
# @Author   : Ann 8194486
# Project   : tj_automation_test

import os.path

# 创建allure报告中environment模块文件的方法
from tools.public.ad import UpdateEdge


def create_env(test_num, allure_path):
    # 定义environment.properties 文件的地址（显示在allure报告中的environment模块）
    env_properties = f"{allure_path}/{test_num}/environment.properties"
    # 创建文件并写入
    with open(env_properties, "w") as f:
        Browser_Version = UpdateEdge().get_version()
        f.write(f"Browser=Edge\nBrowser.Version={Browser_Version}\nPython.Version=3.9.5")


# 创建allure报告中categories模块文件的方法
def create_categories(test_num, allure_path):
    # 定义categories.json 文件的地址
    categories = f"{allure_path}/{test_num}/categories.json"
    # 创建文件并写入
    with open(categories, 'w') as f:
        f.write("""[
          {
            "name": "Ignored tests", 
            "matchedStatuses": ["skipped"] 
          },
          {
            "name": "Infrastructure problems",
            "matchedStatuses": ["broken", "failed"],
            "messageRegex": ".*bye-bye.*" 
          },
          {
            "name": "Outdated tests",
            "matchedStatuses": ["broken"],
            "traceRegex": ".*FileNotFoundException.*" 
          },
          {
            "name": "Product defects",
            "matchedStatuses": ["failed"]
          },
          {
            "name": "Test defects",
            "matchedStatuses": ["broken"]
          }
        ]""")


#
# def set_executors(test_num, allure_path):
#     allure_env_file = os.path.join(allure_path, str(test_num), "html", "widgets", "executors.json")
#     data = [{"name": "Ann", "type": "Jekins", "url": "https://www.baidu.com/",
#              "buildName": "allure-report",
#              "reportUrl": "https://www.baidu.com/",
#              "reportName": "Ann allure report"}]
#     with open(allure_env_file, "w") as file:
#         json.dump(data, file)

# 创建启动allure bat脚本的方法
def render_report(test_num, allure_path):
    # current_folder = os.path.basename(os.path.dirname(os.getcwd()))
    # allure_report_path = allure_path[3:]
    # 定义启动文件地址
    render = f"{allure_path}/{test_num}/html/startup.bat"
    # 创建文件并写入
    with open(render, "w", encoding="utf-8") as f:
        f.write("""@echo off
setlocal enabledelayedexpansion

set "edge_path="
::从注册表查找Edge浏览器路径
set reg_query_command=reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe" /ve

::遍历注册表查询结果
for /f "tokens=2*" %%A in ('%reg_query_command%') do (
    ::如果有REG_SZ，则证明找到了Edge浏览器
    if "%%A"=="REG_SZ" (
        ::如果路径存在，则设置Edge浏览器绝对路径到变量edge_path
        set "tmp_edge_path=%%B"
        if exist "!tmp_edge_path!" (
            set "edge_path=%%B"
        )
    )
)

::如果上面找到了Edge浏览器的路径
if defined edge_path (
    ::打印找到了Edge浏览器的文件地址
    echo Edge found at: "%edge_path%"
    ::带参启动Edge浏览器，使其不校验跨域问题 （./表示当前目录， 可以修改为你所需要的工作目录）
    "%edge_path%" --disable-web-security --user-data-dir="%~dp0/tmp" "%~dp0/index.html"
) else ( 
    ::如果没找到，打印没找到
    echo Edge not found.
    ::打印启动web信息
    echo start a webserver ...
    ::启动一个web服务监听5001端口，在后台运行。默认用当前文件夹的index.html作为首页
    start /b http_server.exe -port 5001
    ::使用默认浏览器打开web服务的地址并等待浏览器关闭
    start "" /WAIT http://127.0.0.1:5001
)
        """)


# 将allure报告转换为PDF和Word文件的方法
def create_report_docx(test_num, allure_path, test_case_name):
    # 定义生成文件位置
    directory_path = f"../report/Word"
    is_exist = os.path.exists(directory_path)
    if not is_exist:
        os.makedirs(directory_path)
        # 命令行输入下列命令 必须有allure_template.docx 和 allure_docx.ini文件，不然会报错不能生成PDF
    os.system(
        f"allure-docx {allure_path}/{test_num} --pdf --template  ../resources/allure_template.docx --config_file ../resources/allure_docx.ini {directory_path}/{test_case_name}.docx")
